﻿#include "setup1wind.h"
#include "ui_setup1wind.h"
#include <mainwindow.h>
extern intListener doorStatus;
QString filep;
QString isc="1";

setup1Wind::setup1Wind(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::setup1Wind)
{

    setWindowIcon(QIcon(":/pic/picture/kafavicon.ico"));
    ui->setupUi(this);
    seldoutpath();

    ui->defaultFileExportMode->setChecked(true);
    connect(&doorStatus, &intListener::valueChanged,this,&setup1Wind::doorStatusChanged,Qt::QueuedConnection);
    ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    ui->pushButton_9->setEnabled(false);
}
void setup1Wind::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
}

setup1Wind::~setup1Wind()
{
    delete ui;
}

void setup1Wind::showEvent(QShowEvent *event)
{
    tcpClient = TcpClient::getInstance();
    if(doorStatus.get()==0)
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    event->accept();
}

void setup1Wind::on_defaultFilePathBtn_clicked()
{
    filePath = QFileDialog::getExistingDirectory(this, "选择文件夹", "/path/to/default/directory");
    if(!filePath.isEmpty()){
        ui->defaultFilePath->clear();
        ui->defaultFilePath->setText(filePath);
    }
}

void setup1Wind::on_defaultFileExportMode_clicked(bool checked)
{
    if(checked){
        isc="1";
    }else {
        isc="0";
    }
}

void setup1Wind::on_returnBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}

void setup1Wind::on_SaveBtn_clicked()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update ItemCode set Name='%1' Where DicCode='%2'").arg(ui->defaultFilePath->toPlainText(),"100");
    query.exec(sql);

    tcts *tctsWindow = new tcts();
    tctsWindow->infochange("提示","treat","保存成功",false,true);
    tctsWindow->show();
    filep=ui->defaultFilePath->toPlainText();
}

void setup1Wind::on_CancelBtn_clicked()
{
    this->close();
    emit changeTotalWindow();
}


void setup1Wind::on_pushButton_9_clicked()
{
    if(doorStatus.get()==0)
    {
        doorStatus.set(1);
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
    else
    {
        doorStatus.set(0);
        ui->pushButton_9->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    }
}
// cnr 6.27 导出路径读取
void setup1Wind::seldoutpath(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="SELECT Name FROM ItemCode WHERE DicCode=100";
    query.exec(sql);
    while (query.next()) {
        QString column1Value = query.value("Name").toString();
        filePath=column1Value;
    }
    if(!filePath.isEmpty()){
        ui->defaultFilePath->setText(filePath);
    }
    filep = filePath;
}
